//判断是否在中国
if(![LocationConverter isLocationOutOfChinaRay:[LocationConverter transformFromGCJToWGS:customLoc2D]])
{
NSLog(@”%@”,reGeocode.formattedAddress);
//查询逆地理编码成功
[ws.headMap setLocationInfo:reGeocode];
//模糊查询本地城市列表
CityInfo *searchInfo = [[CityInfo alloc]init];
if ([CommonHelp isBlankString:reGeocode.addressComponent.city] )
{
//城市名模糊查询
searchInfo.cityName = [NSString stringWithFormat:@”%@”,[reGeocode.addressComponent.province stringByReplacingOccurrencesOfString:@”市” withString:@””]];
}else
{
//城市名模糊查询
searchInfo.cityName = [NSString stringWithFormat:@”%@”,[reGeocode.addressComponent.city stringByReplacingOccurrencesOfString:@”市” withString:@””]];
}
DatabaseManager *manager = [DatabaseManager sharedInstance];
NSArray *cityArr = [manager queryCityWithInfo:searchInfo];
CityInfo *resultInfo;
if(cityArr.count >0)
{
resultInfo = cityArr[0];
}
[USDF setObject:resultInfo.adCode forKey:@"adCode"];
[USDF setObject:reGeocode.formattedAddress forKey:@"currentPosition"];
[USDF setObject:reGeocode.addressComponent.citycode forKey:@"citycode"];
[USDF setObject:[NSString stringWithFormat:@"%f",location.coordinate.latitude] forKey:@"latitude"];
[USDF setObject:[NSString stringWithFormat:@"%f",location.coordinate.longitude] forKey:@"longitude"];
[ws requestListData];//获取数据
}else
{
NSString * latlon= [NSString stringWithFormat:@"&lat=%f&lon=%f",location.coordinate.latitude,location.coordinate.longitude];
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
//网络请求时间
[session.requestSerializer willChangeValueForKey:@"timeoutInterval"];
session.requestSerializer.timeoutInterval = 10;
[session.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[session POST:[NSString stringWithFormat:@"http://nominatim.openstreetmap.org/reverse?zoom=10&addressdetails=1&format=json%@",latlon] parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id result=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"请求成功数据-->%@",result);
NSDictionary * dic = [[NSDictionary alloc] initWithDictionary:[result objectForKey:@"address"]];
NSString * address = [dic objectForKey:@"city"];
NSString * displayName = [NSString stringWithFormat:@"%@",[result objectForKey:@"display_name"]];
[ws.headMap setLocationtext:displayName];
//模糊查询本地城市列表
CityInfo *searchInfo = [[CityInfo alloc]init];
NSString * a = [NSString stringWithFormat:@"%@",[address stringByReplacingOccurrencesOfString:@"特别" withString:@""]];
NSString * locatedAt = [NSString stringWithFormat:@"%@",[a stringByReplacingOccurrencesOfString:@"市" withString:@""]];
//城市名模糊查询
searchInfo.cityName = locatedAt;
DatabaseManager *manager = [DatabaseManager sharedInstance];
NSArray *cityArr = [manager queryCityWithInfo:searchInfo];
CityInfo *resultInfo;
if(cityArr.count >0)
{
resultInfo = cityArr[0];
}
[USDF setObject:resultInfo.adCode forKey:@"adCode"];
[USDF setObject:displayName forKey:@"currentPosition"];
[USDF setObject:resultInfo.adCode forKey:@"citycode"];
[USDF setObject:[NSString stringWithFormat:@"%f",location.coordinate.latitude] forKey:@"latitude"];
[USDF setObject:[NSString stringWithFormat:@"%f",location.coordinate.longitude] forKey:@"longitude"];
[ws requestListData];//获取数据
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//NSLog(@"%@",error);
}];
// [self.geocoder reverseGeocodeLocation: location completionHandler:
// ^(NSArray placemarks, NSError error) {
// if (error == nil) {
// // 系统反地理编码成功
// //得到自己当前最近的地名
// CLPlacemark placemark = [placemarks objectAtIndex:0];
// NSDictionary dic = placemark.addressDictionary;
// NSString locatedAt = placemark.locality;
// NSString ads=[NSString stringWithFormat:@”%@,%@”,placemark.name,placemark.locality];
// [ws.headMap setLocationtext:ads];
//
// //模糊查询本地城市列表
// CityInfo searchInfo = [[CityInfo alloc]init];
// //城市名模糊查询
// searchInfo.cityName = [NSString stringWithFormat:@”%@”,[locatedAt stringByReplacingOccurrencesOfString:@”市” withString:@””]];
// DatabaseManager manager = [DatabaseManager sharedInstance];
// NSArray cityArr = [manager queryCityWithInfo:searchInfo];
// CityInfo resultInfo;
// if(cityArr.count >0)
// {
// resultInfo = cityArr[0];
// }
// if(resultInfo.adCode == nil)
// {
// [USDF setObject:@”110000” forKey:@”adCode”];
// }else
// {
// [USDF setObject:resultInfo.adCode forKey:@”adCode”];
// }
//
// [USDF setObject:ads forKey:@”currentPosition”];
// [USDF setObject:placemark.postalCode forKey:@”citycode”];
// [USDF setObject:[NSString stringWithFormat:@”%f”,location.coordinate.latitude] forKey:@”latitude”];
// [USDF setObject:[NSString stringWithFormat:@”%f”,location.coordinate.longitude] forKey:@”longitude”];
//
// [ws requestListData];//获取数据
//
// } else {
// // 系统自带反地理编码失败,由 Google 接管。
// NSLog(@”123”);
// }
// }];
}